package com.yangtzeu.jwc.business;

import android.content.Context;
import android.os.Build;

import com.google.gson.Gson;
import com.yangtzeu.jwc.activity.MainActivity;
import com.yangtzeu.jwc.domain.MyJson;
import com.yangtzeu.jwc.utils.G;
import com.zjsx.lib.utils.L;
import com.zjsx.lib.utils.PrefUtil;
import com.zjsx.lib.utils.SysInfo;

;

public class AppBusiness extends BaseBusiness {

    public AppBusiness(Context context) {
        super(context);
    }

    public void install(final MCallBack callBack) {
        String path = G.APP_INSTALL;
        addCustomParams("INSTALL[device]", Build.ID);
        addCustomParams("INSTALL[model]", android.os.Build.MODEL);
        addCustomParams("INSTALL[manufacturer]", android.os.Build.MANUFACTURER);
        addCustomParams("INSTALL[version]", SysInfo.getVersion(context));
        Gson gson = new Gson();
        String info = gson.toJson(SysInfo.collectDeviceInfo(context));
        addCustomParams("INSTALL[info]", info);
        doHttpRequest(path, paramsMap, new MCallBack() {

            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                super.onFailure(responseBody, failuremsg, isNoticeDialog);
                callBack.onFailure(responseBody, failuremsg, isNoticeDialog);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, MyJson mj) {
                PrefUtil.setBoolean(G.IS_INFO_UPLOADED, true);
                callBack.onSuccess(responseBody, successMsg, isNoticeDialog, mj);
                L.d(mj.toString());
            }

        });
    }

    public void login() {
        if (MainActivity.student != null) {
            String path = G.APP_LOGIN;
            clearParams();
            addCustomParams("USER[number]", MainActivity.student.getSno());
            addCustomParams("USER[name]", MainActivity.student.getSname());
            addCustomParams("USER[class]", MainActivity.student.getSclass());

            if (MainActivity.student.getSsex() != null) {
                addCustomParams("USER[gender]", MainActivity.student.getSsex());
            }
            doHttpRequest(path, paramsMap, new MCallBack() {
                @Override
                public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, MyJson mj) {

                }

            });
        }
    }

    public void server(final MCallBack callBack) {
        doHttpRequest(G.APP_SERVER, new MCallBack() {
            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                callBack.onFailure(responseBody, failuremsg, isNoticeDialog);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, MyJson mj) {
                callBack.onSuccess(responseBody, successMsg, isNoticeDialog, mj);
                L.d(mj.toString());
            }
        });
    }

    public void feedBack(String number, String content, final MCallBack callBack) {
        clearParams();
        addCustomParams("number", number);
        addCustomParams("content", content);
        doHttpRequest(G.APP_FEEDBACK, paramsMap, new MCallBack() {
            @Override
            public void onProgress(int bytesWritten, int totalSize) {
                callBack.onProgress(bytesWritten, totalSize);
            }

            @Override
            public void onFailure(byte[] responseBody, String failuremsg, boolean isNoticeDialog) {
                callBack.onFailure(responseBody, failuremsg, isNoticeDialog);
            }

            @Override
            public void onSuccess(byte[] responseBody, String successMsg, boolean isNoticeDialog, MyJson mj) {
                if (mj.code == 1) {
                    callBack.onSuccess(responseBody, successMsg, isNoticeDialog, mj);
                } else {
                    callBack.onFailure(responseBody, mj.data, isNoticeDialog);
                }
            }
        });
    }
}
